package de.coe.hendricjabs.friendmap.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import de.coe.hendricjabs.friendmap.ConversationTypes;
import de.coe.hendricjabs.friendmap.server.exceptions.SystemException;
import de.coe.hendricjabs.friendmap.server.exceptions.SystemException.ErrorType;

public class Request implements Serializable {

    private static final long serialVersionUID = 1L;
    private char type;
    private String mobile;
    private HashMap<String, String> coordinates;

    public Request(char t, String mobile) {
	if (ConversationTypes.isConversationType(t)) {
	    type = t;
	} else {
	    throw new SystemException(ErrorType.INVALID_CONVERSATION_FORMAT);
	}

	setMobile(mobile);

    }

    public Request(char t) {
	if (ConversationTypes.isConversationType(t)) {
	    type = t;
	} else {
	    throw new SystemException(ErrorType.INVALID_CONVERSATION_FORMAT);
	}
    }
    public Request() {
        
    }

    /**
     * @return the coordinates
     */
    public HashMap<String, String> getCoordinates() {
	return coordinates;
    }

    /**
     * @return the type
     */
    public char getType() {
	return type;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
	return mobile;
    }

    /**
     * @param mobile
     *            the mobile to set
     */
    public void setMobile(String mobile) {
	this.mobile = mobile;
    }

    public String toJSON() {
	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
	try {
	    File temp = File.createTempFile("jsonResponse", ".tmp");
	    mapper.writeValue(temp, this);
	    BufferedReader fr = new BufferedReader(new FileReader(temp));
	    StringBuffer sb = new StringBuffer();
	    String s;
	    while ((s = fr.readLine()) != null) {
		sb.append(s);
		sb.append(System.lineSeparator());
	    }
	    fr.close();
	    return sb.toString();
	} catch (IOException e) {
	    e.printStackTrace();
	}
	return null;
    }

    public static Request fromJSON(String s) throws JsonParseException, JsonMappingException, IOException {
	File temp = File.createTempFile("jsonRequest", ".tmp");

	BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
	bw.write(s);
	bw.close();

	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
	try {
	    String tag = "\"type\":";
	    char[] buf = s.substring(s.indexOf(tag)+tag.length()+1, s.indexOf(tag)+tag.length()+2).toCharArray();
	    char c = ' ';
	    if (buf.length>0) {
	        c = buf[0];
	    };
	    switch (c) {
	        case ConversationTypes.CT_BROADCAST:
	            return mapper.readValue(temp, BroadcastRequest.class);
	        case ConversationTypes.CT_CONTACT:
	            return mapper.readValue(temp, ContactRequest.class);
	        case ConversationTypes.CT_SINGLE:
	            return mapper.readValue(temp, SingleRequest.class);
	        case ConversationTypes.CT_COORDINATES:
                    return mapper.readValue(temp, CoordinateRequest.class);
                default:
                    throw new SystemException(ErrorType.INVALID_CONVERSATION_FORMAT);
	    }
	    
	} catch (JsonMappingException e) {
	    e.printStackTrace();
	    throw new SystemException(ErrorType.INVALID_CONVERSATION_FORMAT);
	}

    }
}
